package indi.caibo.service_edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import indi.caibo.commonutils.R;
import indi.caibo.service_edu.controller.front.clinet.VodClient;
import indi.caibo.service_edu.entity.EduVideo;
import indi.caibo.service_edu.entity.vo.video.VideoInfoForm;
import indi.caibo.service_edu.mapper.EduVideoMapper;
import indi.caibo.service_edu.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import indi.caibo.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author Bo Cai
 * @since 2021-10-07
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {
    // 注入VodClient
    @Autowired
    private VodClient vodClient;

    @Override
    public boolean getCountByChapterId(String id) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id", id);
        Integer count = baseMapper.selectCount(queryWrapper);
        return null != count && count > 0;
    }

    @Override
    public void saveVideoInfo(VideoInfoForm videoInfoForm) {
        EduVideo video = new EduVideo();
        BeanUtils.copyProperties(videoInfoForm, video);
        boolean result = this.save(video);
        if (!result) {
            throw new GuliException(20001, "课时信息保存失败");
        }
    }

    @Override
    public VideoInfoForm getVideoInfoFormById(String id) {
        //从video表中取数据
        EduVideo video = this.getById(id);
        if (video == null) {
            throw new GuliException(20001, "数据不存在");
        }
        //创建videoInfoForm对象
        VideoInfoForm videoInfoForm = new VideoInfoForm();
        BeanUtils.copyProperties(video, videoInfoForm);
        return videoInfoForm;
    }

    @Override
    public void updateVideoInfoById(VideoInfoForm videoInfoForm) {
        //保存课时基本信息
        EduVideo video = new EduVideo();
        BeanUtils.copyProperties(videoInfoForm, video);
        boolean result = this.updateById(video);
        if (!result) {
            throw new GuliException(20001, "课时信息保存失败");
        }
    }

    @Override
    public boolean removeVideoById(String id) {
        // 根据小节id得到视频ip
        String videoSourceId = baseMapper.selectById(id).getVideoSourceId();
        // 调用vod方法删除视频
        if (!StringUtils.isEmpty(videoSourceId)) {
            R result = vodClient.removeVideo(videoSourceId);
            if (result.getCode() == 20001) {
                throw new GuliException(20001, "删除节失败，视频点播服务不可用（此消息来自熔断器）！");
            }
        }
        int result = baseMapper.deleteById(id);
        return result > 0;
    }

    @Override
    public boolean removeByCourseId(String id) {
        /*1. 根据课程id获取所有视频id*/
        List<String> videoIds = baseMapper.selectVideoIdsByCourseId(id);
        //2. 调用vod服务删除远程视频
        if (videoIds.size() > 0) {
            vodClient.removeVideoList(videoIds);
        }
        /*3. 删除video的记录*/
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", id);
        int count = baseMapper.delete(queryWrapper);
        return count > 0;
    }

    @Override
    public int getCountByCourseId(String courseId) {
        Integer count = baseMapper.selectCount(new QueryWrapper<EduVideo>().eq("course_id", courseId));
        return count == null ? 0 : count;
    }

}
